<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>地址匹配服务</title>
    <link href="http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .control {
            position: absolute;
            top: 10px;
            right: 10px;
        }

        #map {
            position: absolute;
        }
    </style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%">
<div id="map" style="width: 100%;height:100%"></div>
<div id="control" class="control" style='width:20%'>
    <div class="panel panel-default">
        <div class="panel-body">
            <ul class="nav nav-tabs nav-justified">
                <li id="geocode" role="presentation" class="active"><a href="#">正向匹配</a></li>
                <li id="geodecode" role="presentation"><a href="#">反向匹配</a></li>
            </ul>
            <p>
                <div class="tab-content">
                    <div id="geocodeParam" role="tabpanel" class="tab-pane deplistContent active">
                        <div class="input-group">
                            <span class="input-group-addon">地址　　　</span>
                            <input id="address" type="text" class="form-control" placeholder="公司"/>
                        </div>
            <p>
            <div class="input-group">
                <span class="input-group-addon">过滤字段　</span>
                <input id="filters" type="text" class="form-control"
                       placeholder="北京"/>
            </div>
            <p>
            <div class="input-group">
                <span class="input-group-addon">最小索引　</span>
                <input id="fromIndex" type="text" class="form-control"
                       placeholder="0"/>
            </div>
            <p>
            <div class="input-group">
                <span class="input-group-addon">最大索引　</span>
                <input id="toIndex" type="text" class="form-control"
                       placeholder="10"/>
            </div>
            <p>
            <div class="input-group">
                <span class="input-group-addon">最大结果数</span>
                <input id="maxReturn" type="text" class="form-control"
                       placeholder="-1"/>
            </div>
            <p>
            <div class="input-group">
                <span class="input-group-addon">坐标系　　</span>
                <input id="prjCoordSys" type="text" class="form-control"
                       placeholder="{epsgcode:4326}"/>
            </div>
            <p>
            <div align="right">
                <input type="button" id='codeBtn' class="btn btn-primary" value="匹配"/>
            </div>
        </div>
        <div id="geodecodeParam" role="tabpanel" class="tab-pane deplistContent">
            <div class="input-group">
                <span class="input-group-addon">横坐标　　</span>
                <input id="xCoord" type="text" class="form-control" placeholder="116.3518541194752"/>
            </div>
            <p>
            <div class="input-group">
                <span class="input-group-addon">纵坐标　　</span>
                <input id="yCoord" type="text" class="form-control"
                       placeholder="40.00097839595237"/>
            </div>
            <p>
            <div class="input-group">
                <span class="input-group-addon">过滤字段　</span>
                <input id="filters2" type="text" class="form-control"
                       placeholder=""/>
            </div>
            <p>
            <div class="input-group">
                <span class="input-group-addon">最小索引　</span>
                <input id="fromIndex2" type="text" class="form-control"
                       placeholder="0"/>
            </div>
            <p>
            <div class="input-group">
                <span class="input-group-addon">最大索引　</span>
                <input id="toIndex2" type="text" class="form-control"
                       placeholder="10"/>
            </div>
            <p>
            <div class="input-group">
                <span class="input-group-addon">最大结果数</span>
                <input id="maxReturn2" type="text" class="form-control"
                       placeholder="-1"/>
            </div>
            <p>
            <div class="input-group">
                <span class="input-group-addon">查询半径　</span>
                <input id="geoDecodingRadius" type="text" class="form-control"
                       placeholder="-1"/>
            </div>
            <p>
            <div class="input-group">
                <span class="input-group-addon">坐标系　　</span>
                <input id="prjCoordSys2" type="text" class="form-control"
                       placeholder="{epsgcode:4326}"/>
            </div>
            <p>
            <div align="right">
                <input type="button" id='decodeBtn' class="btn btn-primary" value="匹配"/>
            </div>
        </div>
    </div>
</div>
</div>
</div>

<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="http://iclient.supermapol.com/libs/iclient8c/libs/SuperMap.Include.js"></script>
<script type="text/javascript" src="../../dist/iclient9-legacy.min.js"></script>
<script type="text/javascript">
    var map, infowin, layer, markerlayer,
            url = "http://support.supermap.com.cn:8090/iserver/services/map-china400/rest/maps/China_4326",
            addressUrl = "http://117.122.248.69:8090/iserver/services/addressmatch-Address/restjsr/v1/address",
            imgUrl = "http://support.supermap.com.cn:8090/iserver/iClient/forJavaScript/theme/images/marker.png",
            addressMatchService = new SuperMap.REST.AddressMatchService(addressUrl, {data: '11'}),
            map = new SuperMap.Map("map", {
                controls: [
                    new SuperMap.Control.Navigation(),
                    new SuperMap.Control.Zoom()
                ]
            });
    map.addControl(new SuperMap.Control.MousePosition());

    layer = new SuperMap.Layer.TiledDynamicRESTLayer("China", url, null, {maxResolution: "auto"});
    markerlayer = new SuperMap.Layer.Markers("markerLayer");
    layer.events.on({"layerInitialized": addLayers});

    function addLayers() {
        map.addLayers([layer, markerlayer]);
        map.setCenter(new SuperMap.LonLat(116.383572, 39.914714), 10);
    }

    $("#geocode").click(function () {
        $("#geocode")[0].className = 'active';
        $("#geodecode")[0].className = '';
        $("#geocodeParam").addClass('active');
        $("#geodecodeParam").removeClass('active');
    });
    $("#geodecode").click(function () {
        $("#geocode")[0].className = '';
        $("#geodecode")[0].className = 'active';
        $("#geodecodeParam").addClass('active');
        $("#geocodeParam").removeClass('active');
    });
    //判断输入字符串是否为空或者全部都是空格
    function isNull(str) {
        if (str == "") return true;
        var regu = "^[ ]+$";
        var re = new RegExp(regu);
        return re.test(str);
    }
    var filters = $('#filters').val() || $('#filters').attr('placeholder');
    $("#codeBtn").click(function () {
        var geoCodeParam = new SuperMap.GeoCodingParameter({
            address: $('#address').val() || $('#address').attr('placeholder'),
            fromIndex: $('#fromIndex').val() || $('#fromIndex').attr('placeholder'),
            toIndex: $('#toIndex').val() || $('#toIndex').attr('placeholder'),
            filters: isNull(filters) ? '' : filters,
            prjCoordSys: $('#prjCoordSys').val() || $('#prjCoordSys').attr('placeholder'),
            maxReturn: $('#maxReturn').val() || $('#maxReturn').attr('placeholder')
        });
        addressMatchService.code(geoCodeParam, match);
    });
    $("#decodeBtn").click(function () {
        var geoDecodeParam = new SuperMap.GeoDecodingParameter({
            x: $('#xCoord').val() || $('#xCoord').attr('placeholder'),
            y: $('#yCoord').val() || $('#yCoord').attr('placeholder'),
            fromIndex: $('#fromIndex2').val() || $('#fromIndex2').attr('placeholder'),
            toIndex: $('#toIndex2').val() || $('#toIndex2').attr('placeholder'),
            filters: $('#filters2').val() || $('#filters2').attr('placeholder'),
            prjCoordSys: $('#prjCoordSys2').val() || $('#prjCoordSys2').attr('placeholder'),
            maxReturn: $('#maxReturn2').val() || $('#maxReturn2').attr('placeholder'),
            geoDecodingRadius: $('#geoDecodingRadius').val() || $('#geoDecodingRadius').attr('placeholder'),
        });
        addressMatchService.decode(geoDecodeParam, match);
    });

    function match(obj) {
        closeInfoWin();
        markerlayer.clearMarkers();
        obj.result.map(function (item) {
            var size = new SuperMap.Size(44, 33);
            var offset = new SuperMap.Pixel(-(size.w / 2), -size.h);
            var icon = new SuperMap.Icon(imgUrl, size, offset);
            var marker = new SuperMap.Marker(new SuperMap.LonLat(item.location.x, item.location.y), icon);
            marker.events.on({
                "click": openInfoWin,
                "scope": marker
            });
            markerlayer.addMarker(marker);
            map.setCenter(new SuperMap.LonLat(116.383572, 39.914714), 8);

            function openInfoWin() {
                closeInfoWin();
                var lonlat = marker.getLonLat();
                var size = new SuperMap.Size(0, 33);
                var offset = new SuperMap.Pixel(11, -30);
                var icon = new SuperMap.Icon(imgUrl, size, offset);
                var innerHTML = "";
                innerHTML += "地址:" + item.address + "<br>";
                var x = Number(item.location.x.toString().match(/^\d+(?:\.\d{0,2})?/));
                var y = Number(item.location.y.toString().match(/^\d+(?:\.\d{0,2})?/));
                innerHTML += "坐标:[" + x + "," + y + "]<br>";
                if (item.score > 0) {
                    innerHTML += "匹配度:" + item.score + "<br>";
                }
                innerHTML += "过滤字段:" + item.filters + "<br>";
                var popup = new SuperMap.Popup.FramedCloud("popwin",
                        new SuperMap.LonLat(lonlat.lon, lonlat.lat),
                        null,
                        innerHTML,
                        icon,
                        true);
                infowin = popup;
                map.addPopup(popup);
            }

        });

        //关闭信息框
        function closeInfoWin() {
            if (infowin) {
                try {
                    infowin.hide();
                    infowin.destroy();
                }
                catch (e) {
                }
            }
        }
    }
</script>
</body>
</html>